package com.gdut.simple.common;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class ToastUtils {

    private static Handler mHandler = new Handler(Looper.getMainLooper());
    private static Toast mToast = null;
    private static final Object mSyncObject = new Object();

    public static void showShort(Context context, String message) {
        show(context, message, Toast.LENGTH_SHORT);
    }

    public static void showLong(Context context, String message) {
        show(context, message, Toast.LENGTH_LONG);
    }

    private static void show(final Context context, final String message, final int duration) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (mSyncObject) {
                            if (mToast != null) {
                                // mToast.cancel();
                                mToast.setText(message);
                                mToast.setDuration(duration);
                            } else {
                                mToast = Toast.makeText(context, message, duration);
                            }
                            mToast.show();
                        }
                    }
                });
            }
        }).start();
    }

}
